define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var time = Fast.api.query('time', '');
            var is_pay = Fast.api.query('is_pay', '');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'voucher/order/index' + location.search,
                    add_url: 'voucher/order/add',
                    edit_url: 'voucher/order/edit',
                    del_url: 'voucher/order/del',
                    multi_url: 'voucher/order/multi',
                    import_url: 'voucher/order/import',
                    table: 'voucher_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE'},
                        {field: 'user.city', title: __('City'), operate: 'LIKE'},
                        {field: 'user.region', title: __('Region'), operate: 'LIKE'},
                        {field: 'factory_nickname', title: __('Factory.nickname'), operate: false },
                        {field: 'user.factory_id', title: __('Factory.nickname'), visible: false, searchList: $.getJSON("factory/factory/select")},
                        {field: 'shop_nickname', title: __('Shop.nickname'), operate: false },
                        {field: 'user.shop_id', title: __('Shop.nickname'), visible: false, searchList: $.getJSON("shop/select")},
                        {field: 'company_nickname', title: __('Company.nickname'), operate: false },
                        {field: 'user.company_id', title: __('Company.nickname'), visible: false, searchList: $.getJSON("auth/admin/select")},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'send_money', title: __('Send_money'), operate:'BETWEEN'},
                        {field: 'is_pay', title: __('Is_pay'), defaultValue: is_pay ? is_pay : ''},
                        {field: 'pay_time', title: __('Pay_time'), defaultValue: time ? time : '', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
